﻿using Common.Contract.Paging;
using Common.Contract.SalePerformance.Record;
using Common.Contract.SalePerformance.Request;
using Common.Contract.SalePerformance.Response;
using Common.Service.Command;
using SalePerformance.Model.Interface;
using SalePerformance.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SalePerformance.Core.ServiceCommand
{
    public class RetrieveDepositByDaoServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveSalePerformanceByDaoRequest, RetrieveSalePerformanceByDaoResponse>
    {
        #region Properties
        public string Dao;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveDepositByDaoServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveSalePerformanceByDaoRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.Dao))
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.Dao;
            }
        }

        protected override bool OnExecute(RetrieveSalePerformanceByDaoResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<SalePerformanceRecord> records = new List<SalePerformanceRecord>();
                List<TblSpDepositEop> listDeposits = new List<TblSpDepositEop>();
                listDeposits = Providers.SalePerformance.RetrieveDepositByDao(Dao);
                if (listDeposits != null)
                {
                    List<TblSpDepositEop> listQuantity = listDeposits.Where(x => x.Col == "quantity").Select(x => x).ToList();
                    List<TblSpDepositEop> listBalance = listDeposits.Where(x => x.Col == "balance").Select(x => x).ToList();

                    records = (from a in listQuantity
                               join b in listBalance
                               on new { a.Dao, a.ProductNameQd, a.GroupType } equals new { b.Dao, b.ProductNameQd, b.GroupType }
                               select new SalePerformanceRecord
                               {
                                   Col = a.Col,
                                   Dao = a.Dao,
                                   GroupType = a.GroupType,
                                   ProductNameQd = a.ProductNameQd,
                                   month0 = a.Month0,
                                   month1 = a.Month1,
                                   month2 = a.Month2,
                                   month3 = a.Month3,
                                   month4 = a.Month4,
                                   month5 = a.Month5,
                                   month6 = a.Month6,
                                   month7 = a.Month7,
                                   month8 = a.Month8,
                                   month9 = a.Month9,
                                   month10 = a.Month10,
                                   month11 = a.Month11,
                                   month0b = ((double)b.Month0 / 1e9).ToString("0.00"),
                                   month1b = ((double)b.Month1 / 1e9).ToString("0.00"),
                                   month2b = ((double)b.Month2 / 1e9).ToString("0.00"),
                                   month3b = ((double)b.Month3 / 1e9).ToString("0.00"),
                                   month4b = ((double)b.Month4 / 1e9).ToString("0.00"),
                                   month5b = ((double)b.Month5 / 1e9).ToString("0.00"),
                                   month6b = ((double)b.Month6 / 1e9).ToString("0.00"),
                                   month7b = ((double)b.Month7 / 1e9).ToString("0.00"),
                                   month8b = ((double)b.Month8 / 1e9).ToString("0.00"),
                                   month9b = ((double)b.Month9 / 1e9).ToString("0.00"),
                                   month10b = ((double)b.Month10 / 1e9).ToString("0.00"),
                                   month11b = ((double)b.Month11 / 1e9).ToString("0.00"),
                               }).OrderBy(x => x.ProductNameQd).Select(x => x).ToList();
                }
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = 0,
                    PageSize = records.Count,
                    TotalRecords = records.Count
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;

            }

            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
